// vuex 的权限管理模块

// 引入静态路由
import { constantRoutes, asyncRoutes } from '@/router'

// vuex中的permission模块用来存放当前的 静态路由 + 当前用户的 权限路由
const state = {
  routes: []
}

const mutations = {
// 合并静态路由与筛选出来的权限路由
  setRoutes(state, asyncRoutes) {
    // 显示的菜单应该由静态路由及根据权限筛选过的路由来渲染
    const newRoutes = [...constantRoutes, ...asyncRoutes]
    // 将处理过的路由交给state
    state.routes = newRoutes
  }
}
const actions = {
// 封装一个筛选权限路由的方法
  filterRouter(store, menus) {
    //   筛选路由 与传入的数据去与路由的name去匹配
    const routes = asyncRoutes.filter(routes => menus.indexOf(routes.name) !== -1)
    console.log(routes)
    // 将筛选过的路由提交给mutations
    store.commit('setRoutes', routes)
    return routes
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
